package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.Invoice;
import com.itheima.domain.cargo.InvoiceExample;
import com.itheima.domain.cargo.Packing;
import com.itheima.service.cargo.InvoiceService;
import com.itheima.service.cargo.PackingService;
import com.itheima.service.cargo.ShippingService;
import com.itheima.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {
    @Reference
    private ShippingService shippingService; //委托单
    @Reference
    private InvoiceService invoiceService;//发票


    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/toInvoice", name = "跳转发票新增页面")
    public String toInvoice(String id) {
        request.setAttribute("id",id);
        Packing packing = packingService.findById(id);
        Invoice invoice = new Invoice();
        invoice.setJine(packing.getFeiyong());
        invoice.setHetonghao(packing.getExportIds());

        request.setAttribute("invoice",invoice);

        return "cargo/invoice/invoice-add";
    }


    //添加 或 修改
    @RequestMapping("/edit")
    public String edit(Invoice invoice){
        System.out.println(invoice);
        invoice.setState(0);
        invoiceService.save(invoice);

        return "redirect:/cargo/invoice/list.do";
    }
    //分页显示
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize){
        InvoiceExample invoiceExample = new InvoiceExample();
        PageInfo info = invoiceService.findByPage(pageNum, pageSize,invoiceExample);
        request.setAttribute("page", info);
        return "/cargo/invoice/invoice-list";
    }


    @RequestMapping(value = "/delete", name = "发票单删除")
    public String delete(String id) {
        //调用service删除
        invoiceService.delete(id);

        //重定向到list方法
        return "redirect:/cargo/invoice/list.do";
    }
}
